rm(list=ls())

load("TSCS_data.RData")
source("02_function.R")

# Figure 2

lm_down_1.2 <- plm(downturn_polyarchy ~ lag(downturn_polyarchy,1:2)  +
                     antipluralism_gov_seat_share_lag*citizen_support_lag + 
                     lag(polarization,1) +
                     lag(clientelism_gov_seat_share,1) + 
                     lag(region_citizen_support,1) + 
                     lag(region_antipluralism_gov_seat_share,1) + 
                     lag(region_polyarchy,1) + 
                     lag(democratic_stock,1) + 
                     lag(GDP_capita,1) + 
                     lag(life_expectancy,1),
                   data = panel,
                   model = "within",
                   effect = "twoways")

marginal <- interaction_plot_continuous_ggplot(lm_down_1.2,
                                               "antipluralism_gov_seat_share_lag", 
                                               "citizen_support_lag", 
                                               "antipluralism_gov_seat_share_lag:citizen_support_lag", 
                                               conf=.95,
                                               title="", xlabel="Citizen Support for Democracy",
                                               ylabel="Marginal Effect of Anti-pluralist Government") +
  scale_x_continuous(limits = c(-1.1, 1.1)) +
  scale_y_continuous(labels = label_number(accuracy = .1))

density <- ggplot(as.data.frame(panel),aes(x=citizen_support_lag)) + 
  geom_density(adjust=1.5, alpha=.4) +
  ylab("Density") + 
  xlab("") +
  scale_y_continuous(labels = label_number(accuracy = .01)) +
  scale_x_continuous(limits = c(-1.1, 1.1)) +
  theme(legend.position = "none")

full_plot <- ggarrange(marginal,density,
                       nrow = 2,ncol = 1,heights = c(4,1))
full_plot

pdf(file = "fig_d1.pdf",width = 8,height = 5)
full_plot
dev.off()
